/**
 * FileName: FinalizeTest
 * Author:   powersi
 * Date:     2021/3/18 15:37
 * Description:
 */
package com.zm.demo.core.gc;

/**
 * Description: 
 * Author:      powersi
 * Date:        2021/3/18 15:37
 */
public class FinalizeTest {

    private static FinalizeTest finalizeTest;

    public static void main(String[] args) throws Exception {
        finalizeTest = new FinalizeTest();
        finalizeTest = null;
        System.gc();
        Thread.sleep(1000L);
        if (finalizeTest == null){
            System.out.println("没有拯救1");
        } else {
            System.out.println("被finalize()拯救了1");
        }

        finalizeTest = null;
        System.gc();
        Thread.sleep(1000L);
        if (finalizeTest == null){
            System.out.println("没有拯救2");
        } else {
            System.out.println("被finalize()拯救了2");
        }

    }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("执行finalize()方法");
        FinalizeTest.finalizeTest = this;
    }
}
